package com.maniksinghal4686.timedsilencer;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.media.AudioManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class SilenceOptionsActivity extends Activity {
	
	final static String LOG = "mylog";
	final static String PREF_FILE = "com.maniksinghal4686.timedsilencer.PREF_FILE";
	final static String PREF_VIEW_MODE = "com.maniksinghal4686.timedsilencer.VIEW_MODE";
	
	int audio_state;
	Calendar half, one, one_half, two, two_half, three;

	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		TextView view;
		int view_id;
		super.onCreate(savedInstanceState);
		sendBroadcast(new Intent(Intent.ACTION_MAIN)
	       .addCategory(Intent.CATEGORY_HOME));
		setContentView(R.layout.activity_silence_options);
		
		SharedPreferences sharedPref = this.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
		if (sharedPref != null) {
			view_id = sharedPref.getInt(PREF_VIEW_MODE,  R.id.radio_vibrator);
		} else {
			view_id = R.id.radio_vibrator;
		}
		
		
		// Set default options
		RadioButton radio = (RadioButton)findViewById(view_id);
		radio.setChecked(true);
		
		if (view_id == R.id.radio_vibrator) {
			audio_state = AudioManager.RINGER_MODE_VIBRATE;	
		} else {
			audio_state = AudioManager.RINGER_MODE_SILENT;
		}
		
		
		// Start the service
		Log.v(LOG, "Started the activity");
		
		// Initialize the precise-duration text views
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		int minute = cal.get(Calendar.MINUTE);
		if (minute >= 30) {
			minute = minute - 30;
		}
		cal.add(Calendar.MINUTE,  -(minute));
		
		half = Calendar.getInstance();
		half.setTimeInMillis(cal.getTimeInMillis());
		half.add(Calendar.MINUTE, 30);
		view = (TextView) findViewById(R.id.radio_absolute_half);
		view.setText("till " + new SimpleDateFormat("HH:mm").format(half.getTime()) + "  ");
		
		one = Calendar.getInstance();
		one.setTimeInMillis(cal.getTimeInMillis());
		one.add(Calendar.MINUTE,  60);
		view = (TextView) findViewById(R.id.radio_absolute_one);
		view.setText("till " + new SimpleDateFormat("HH:mm").format(one.getTime()) + "  ");
		
		one_half = Calendar.getInstance();
		one_half.setTimeInMillis(cal.getTimeInMillis());
		one_half.add(Calendar.MINUTE,  90);
		view = (TextView) findViewById(R.id.radio_absolute_one_half);
		view.setText("till " + new SimpleDateFormat("HH:mm").format(one_half.getTime()) + "  ");

		two = Calendar.getInstance();
		two.setTimeInMillis(cal.getTimeInMillis());
		two.add(Calendar.MINUTE,  120);
		view = (TextView) findViewById(R.id.radio_absolute_two);
		view.setText("till " + new SimpleDateFormat("HH:mm").format(two.getTime()) + "  ");

		two_half = Calendar.getInstance();
		two_half.setTimeInMillis(cal.getTimeInMillis());
		two_half.add(Calendar.MINUTE,  150);
		view = (TextView) findViewById(R.id.radio_absolute_two_half);
		view.setText("till " + new SimpleDateFormat("HH:mm").format(two_half.getTime()) + "  ");
		
		three = Calendar.getInstance();
		three.setTimeInMillis(cal.getTimeInMillis());
		three.add(Calendar.MINUTE,  180);
		view = (TextView) findViewById(R.id.radio_absolute_three);
		view.setText("till " + new SimpleDateFormat("HH:mm").format(three.getTime()) + "  ");
		

	}
	
	public void onSilenceTimeClick(View view) {
		RadioButton radio = (RadioButton)view;
		Calendar cal;
		long ms;
		
		
		switch(view.getId()) {
		case R.id.radio_absolute_half:
			ms = half.getTimeInMillis();
			break;
		case R.id.radio_absolute_one:
			ms = one.getTimeInMillis();
			break;
		case R.id.radio_absolute_one_half:
			ms = one_half.getTimeInMillis();
			break;
		case R.id.radio_absolute_two:
			ms = two.getTimeInMillis();
			break;
		case R.id.radio_absolute_two_half:
			ms = two_half.getTimeInMillis();
			break;
		case R.id.radio_absolute_three:
			ms = three.getTimeInMillis();
			break;
		case R.id.radio_relative_half:
			ms = System.currentTimeMillis() + (30 * 60 * 1000);
			break;
		case R.id.radio_relative_one:
			ms = System.currentTimeMillis() + (60 * 60 * 1000);
			break;
		case R.id.radio_relative_one_half:
			ms = System.currentTimeMillis() + (90 * 60 * 1000);
			break;
		case R.id.radio_relative_two:
			ms = System.currentTimeMillis() + (120 * 60 * 1000);
			break;
		case R.id.radio_relative_two_half:
			ms = System.currentTimeMillis() + (150 * 60 * 1000);
			break;	
		default:
		case R.id.radio_other_forever:
			ms = 0;
			break;
		}
		
		if (radio.isChecked()) {
			// Send result to the widget
			Context context = getBaseContext();
			Intent i = new Intent(context, TimedSilenceProvider.class);
			i.setAction(TimedSilenceProvider.USER_CHOICE);
			i.putExtra(TimedSilenceProvider.USER_MODE, audio_state);
			i.putExtra(TimedSilenceProvider.USER_MILLIS, ms);
			context.sendBroadcast(i);
			
			// Display Toast
			String display;
			if (audio_state == AudioManager.RINGER_MODE_SILENT) {
				display = "Silent";
			} else {
				display = "Vibrate";
			}
			
			display += " mode set ";
			if (ms != 0) {
				
				display += " till ";
				
				cal = Calendar.getInstance();
				cal.setTimeInMillis(ms);
				display += new SimpleDateFormat("HH:mm", java.util.Locale.getDefault()).format(cal.getTime());				
				
			} else {
				display += " permanantly.";
			}
			
			Toast toast = Toast.makeText(context,  display, Toast.LENGTH_SHORT);
			toast.show();
			
			finish();
		}
				
	}
	
	public void onSilenceModeClick(View view) {
		
		RadioButton radio = (RadioButton) view;
		int view_id = view.getId();
		boolean checked = radio.isChecked();
		
		if (view_id == R.id.radio_silencer) {
			if (checked) {
				// Silencer selected
				Log.v(LOG, "Silencer selected");
				audio_state = AudioManager.RINGER_MODE_SILENT;
			}
		} else {
			// Vibrator
			if (checked) {
				Log.v(LOG, "Vibrator selected");
				audio_state = AudioManager.RINGER_MODE_VIBRATE;
			}
		}
		
		SharedPreferences sharedPref = this.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPref.edit();
    	editor.putInt(PREF_VIEW_MODE, view_id);
    	editor.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.silence_options, menu);
		return true;
	}

}
